package jp.co.hcs.attendancemanagement.dialog.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.action.AMAction;
import jp.co.hcs.attendancemanagement.dialog.logic.DivisionListLogic;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.DivisionDTO;
import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DivisionListAction extends AMAction {

  public ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    HttpSession session = request.getSession();
    String userId = (String) session.getAttribute(AMConstants.USER_LOGGED_IN);

    response.setContentType("text/json");

    DivisionListLogic logic = new DivisionListLogic();
    List<DivisionDTO> listDivisions = logic.getDivisionListByUserId(userId);

    JSONArray jsonArray = JSONArray.fromObject(listDivisions);

    PrintWriter pw = response.getWriter();
    pw.write("{\"listDivisions\":" + jsonArray.toString() + "}");
    pw.flush();
    pw.close();

    return null;
  }

}
